package cn.edu.ujn.park.controller;

import cn.edu.ujn.park.dao.Book;
import cn.edu.ujn.park.dao.User;
import cn.edu.ujn.park.service.IBookService;
import cn.edu.ujn.park.service.IRateService;
import cn.edu.ujn.park.service.IUserService;
import cn.edu.ujn.util.Page;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class BookController {

    @Autowired
    private IBookService bookService;
    @Autowired
    private IRateService rateService;

    @Autowired
    private IUserService userService;
    @GetMapping("/book")
    public String list(Model model, @RequestParam(value = "page",defaultValue = "1")Integer page ,
                       @RequestParam(value = "row",defaultValue = "10") Integer rows, @ModelAttribute Book book){
        book.setStart((page-1)*rows);
        book.setRows(rows);
        if("".equals(book.getUsername())){
            book.setUsername(null);
        }
        if("".equals(book.getStatus())){
            book.setStatus(null);
        }
        List<User> userList = userService.select();
        Page<User> page1 = bookService.selectAll(book);
        page1.setPage(page);
        model.addAttribute("page",page1);
        model.addAttribute("userList",userList);
        return "adminBookInfo";
    }
    @GetMapping("/bookself")
    public String list2(Model model, @RequestParam(value = "page",defaultValue = "1")Integer page ,
                        @RequestParam(value = "row",defaultValue = "10") Integer rows, @ModelAttribute Book book, HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        book.setUserId(userId);
        book.setStart((page-1)*rows);
        book.setRows(rows);
        if("".equals(book.getUsername())){
            book.setUsername(null);
        }
        if("".equals(book.getStatus())){
            book.setStatus(null);
        }
        Page<User> page1 = bookService.selectAll(book);
        List<User> userList = new ArrayList<>();
        userList.add(userService.getUserById(userId));
        page1.setPage(page);
        model.addAttribute("page",page1);
        model.addAttribute("userList",userList);
        return "userBookInfo";
    }

    @PostMapping("/book")
    @ResponseBody
    public String create(@RequestBody Book book){

        bookService.insert(book);

        return "OK";
    }

    /**
     * 根据id获得book对象
     * @param id
     * @return
     */
    @GetMapping("/book/{id}")
    @ResponseBody
    public Book editBook(@PathVariable("id") Integer id){
        Book book = bookService.getBookById(id);
        return book;
    }

    /**
     * 更新book对象
     * @param book
     * @return
     */
    @PutMapping("/book")
    @ResponseBody
    public String updateBook(@RequestBody Book book){

        bookService.updateByPrimaryKey(book);

        return "OK";
    }

    /**
     * 删除book
     * @param id
     * @return
     */
    @DeleteMapping("/book/{id}")
    @ResponseBody
    public String deleteBook(@PathVariable("id") Integer id){

        bookService.deleteByPrimaryKey(id);
        return "OK";
    }
}
